package me.firstapp.service.shop.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.module.shop.ShoppingCart;
import me.firstapp.module.shop.ShoppingCartItem;
import me.firstapp.repository.shop.ShoppingCartItemRepository;
import me.firstapp.repository.shop.ShoppingCartRepository;
import me.firstapp.service.shop.ShoppingCartService;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Autowired
	private ShoppingCartRepository shoppingCartRepository;

	@Autowired
	private ShoppingCartItemRepository shoppingCartItemRepository;

	@Override
	public ShoppingCart findMemberShoppingCart(Long memberId, Boolean isFindItems, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "会员id");
		}
		if (isFindItems == null) {
			isFindItems = false;
		}
		ShoppingCart shoppingCart = shoppingCartRepository.findMemberShoppingCart(memberId, apiClientId);
		if (shoppingCart != null && isFindItems) {
			List<ShoppingCartItem> items = shoppingCartItemRepository.findShoppingCartItemList(shoppingCart.getId());
			shoppingCart.setItems(items);
		}
		return shoppingCart;
	}

}
